package com.volunteer.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.volunteer.common.R;
import com.volunteer.service.CollectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 收藏控制器
 */
@Tag(name = "收藏管理")
@Slf4j
@RestController
@RequestMapping("/api/activity")
@RequiredArgsConstructor
public class CollectionController {

    private final CollectionService collectionService;

    /**
     * 收藏/取消收藏活动
     *
     * @param activityId 活动ID
     * @param type       操作类型：1-收藏，0-取消收藏
     * @return 操作结果
     */
    @Operation(summary = "收藏/取消收藏活动")
    @PostMapping("/collect")
    public R<Boolean> toggleCollection(
            @Parameter(description = "活动ID") @RequestParam Long activityId,
            @Parameter(description = "操作类型：1-收藏，0-取消收藏") @RequestParam Integer type
    ) {
        boolean result = collectionService.toggleCollection(activityId, type);
        return R.ok(result);
    }


    /**
     * 检查用户是否收藏活动
     */
    @Operation(summary = "检查用户是否收藏活动")
    @GetMapping("/check/{activityId}")
    public R<Boolean> isUserCollected(
            @Parameter(description = "活动ID") @PathVariable Long activityId
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = collectionService.isUserCollected(activityId, userId);
        return R.ok(result);
    }

    /**
     * 统计用户收藏数
     */
    @Operation(summary = "统计用户收藏数")
    @GetMapping("/count/user")
    public R<Integer> countUserCollections() {
        Long userId = StpUtil.getLoginIdAsLong();
        int count = collectionService.countUserCollections(userId);
        return R.ok(count);
    }

    /**
     * 统计活动收藏数
     */
    @Operation(summary = "统计活动收藏数")
    @GetMapping("/count/activity/{activityId}")
    public R<Integer> countActivityCollections(
            @Parameter(description = "活动ID") @PathVariable Long activityId
    ) {
        int count = collectionService.countActivityCollections(activityId);
        return R.ok(count);
    }
}
